<?php
namespace app\model\score;

use think\Model;

class ScoreModel extends Model
{
    protected $name = 'score';

    public static function getAllByType($type) {
        return self::where("type",$type)->select();
    }

    public static function saveData($type,$uid,$data) {
        $s = self::where("uid",$uid)->where("type",$type)->find();
        if ($s == null) {
            $s = new ScoreModel();
            $s['time'] = time();
        }
        $s['score'] = $data;
        $s['type'] = $type;
        $s['uid'] = $uid;
        $s->save();
    }

    public static function typeExist($type) {
        return self::where("type",$type)->count()!=0;
    }

    public static function getUserScore($uid) {
        return self::where("uid",$uid)->order("time","desc")->select();
    }
}

/** 
 * id       成绩ID
 * type     哪一场考试
 * time     保存时间，方便回滚
 * score    成绩（使用json记录）
 * */  
